package net.ziyoushu.demo1.context;

import net.ziyoushu.demo1.config.RocketMqProperties;
import net.ziyoushu.demo1.holder.AsynMessageProduceHoler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

public class RocketMqProduceContext{
    private static final Logger LOGGER = LoggerFactory.getLogger(RocketMqProduceContext.class);

    @Autowired
    RocketMqProperties rocketMqProperties;

   /** 构建消息生成端
    * @Title
    * @Author lcb
    * @Date 2019/3/30
    **/
    @Bean
    public AsynMessageProduceHoler getRocketMqProduce() {
        AsynMessageProduceHoler producer = new AsynMessageProduceHoler(rocketMqProperties.getProducerGroupName());
        try{
            producer.setNamesrvAddr(rocketMqProperties.getNamesrvAddr());
            producer.setInstanceName(rocketMqProperties.getInstanceName());
            producer.setVipChannelEnabled(false);
            producer.start();
        }catch (Exception e) {
            LOGGER.error("producer startup failed" + rocketMqProperties.getInstanceName());
        }
        LOGGER.info("producer startup success ~" + rocketMqProperties.getInstanceName());
        return producer;
    }
}
